
import bpy

from 全局控制.Node.节点和插座基类 import bl_NT_Update
from 全局控制.Node.节点和插座基类 import bl_容器节点

from 节点.节点更新 import update节点


from 场景.场景资源管理 import bl_场景管理

import engine.initLIB.load_dll as engineLib
import ctypes






g_是否开启节点数更新 = False

class C_ops_节点树更新开关(bpy.types.Operator):
	bl_idname = 'node.update_node_tree_off_and_on'
	bl_label = '节点树更新开关'

	m_是否开启节点树更新 = bpy.props.StringProperty(name='是否开启节点树更新', default='')
	def execute(self, context):
		global g_是否开启节点数更新
		
		if g_是否开启节点数更新 :
			g_是否开启节点数更新 = False
		else:
			g_是否开启节点数更新 = True

		return {'FINISHED'}



class C_ops_更新被动节点树(bpy.types.Operator):
	bl_idname = 'node.update_node_tree_passive_update'
	bl_label = '更新被动节点树'

	m_场景 = bpy.props.StringProperty(name='场景', default='')
	m_节点 = bpy.props.StringProperty(name='节点', default='')
	def execute(self, context):
		tree = bpy.data.node_groups[self.m_场景]
		print('opera.m_节点 = ', self.m_节点)
		tree.f_定点更新([ tree.nodes[self.m_节点] ])
		return {'FINISHED'}



class C_ops_更新被动节点树_渲染下一帧(bpy.types.Operator):
	bl_idname = 'node.update_node_tree_passive_update_render'
	bl_label = '更新被动节点树'

	m_场景 = bpy.props.StringProperty(name='场景', default='')
	m_节点 = bpy.props.StringProperty(name='节点', default='')
	def execute(self, context):
		tree = bpy.data.node_groups[self.m_场景]
		for i in range(bpy.context.scene.frame_start, bpy.context.scene.frame_end):
			bpy.context.scene.frame_current += 1
			tree.f_定点更新([ tree.nodes[self.m_节点] ])
			bpy.ops.render.render(use_viewport=True)
		return {'FINISHED'}

















class C_ops_重新构建当前场景树(bpy.types.Operator):
	bl_idname = 'node.update_node_rebuild_current_tree'
	bl_label = '重新构建当前节点树'

	m_场景 = bpy.props.StringProperty(name='场景', default='')
	def execute(self, context):
		bl_场景管理.f_轮询场景(bpy.context.scene)
		
		tree = bpy.data.node_groups[self.m_场景]
		tree.f_重新构建节点()
		return {'FINISHED'}





class C_ops_重新构建所有场景树(bpy.types.Operator):
	bl_idname = 'node.update_node_rebuild_all_tree'
	bl_label = '重新构建所有节点树'
	
	def execute(self, context):
		update节点.f_初始化更新(context.scene)
		#engineLib.flexCAPI.f_node_初始化更新节点()
		'''if len(bpy.app.handlers.frame_change_pre) <= 0:
			bpy.app.handlers.frame_change_pre.append(update节点.f_帧更新)'''
		return {'FINISHED'}






class C_ops_重置所有节点更新计时(bpy.types.Operator):
	bl_idname = 'node.update_reset_all_node_update_timings'
	bl_label = '重置所有节点更新计时'

	m_场景 = bpy.props.StringProperty(name='场景', default='')
	def execute(self, context):
		#tree = bpy.data.node_groups[self.m_场景]
		engineLib.flexCAPI.f_重置所有节点更新计时(ctypes.c_wchar_p(str(self.m_场景)))
		return {'FINISHED'}


'''class C_ops_更新指定被动节点树(bpy.types.Operator):
	bl_idname = 'node.update_node_tree_specified_passive_update'
	bl_label = '更新被动节点树'

	m_场景 = bpy.props.StringProperty(name='场景', default='')
	m_节点 = bpy.props.StringProperty(name='节点', default='')
	def execute(self, context):
		tree = bpy.data.node_groups[self.m_场景]
		tree.f_定点更新(self.m_节点)
		return {'FINISHED'}'''








g_当前节点树 = None

class NODE_PT_active_节点树更新配置(bpy.types.Panel):
	bl_space_type = 'NODE_EDITOR'
	bl_region_type = 'UI'
	bl_label = "节点树更新配置"

	@classmethod
	def poll(cls, context):
		#print('cls = ', cls)
		global g_当前节点树
		if context.active_node :
			g_当前节点树 = context.active_node.id_data
		return True

	def draw(self, context):
		layout = self.layout
		node = context.active_node
		global g_当前节点树

		if g_当前节点树 :
			col = layout.column()
			col.prop(g_当前节点树, 'm_当前绑定场景')
			#opera = layout.operator("node.update_node_rebuild_current_tree", text='重新构建场景树', icon='MOD_BUILD')
			#opera.m_场景 = g_当前节点树.name
			layout.operator('node.update_node_rebuild_all_tree', icon='MOD_BUILD')




class NODE_PT_active_激活节点配置(bpy.types.Panel):
	bl_space_type = 'NODE_EDITOR'
	bl_region_type = 'UI'
	bl_label = "激活节点配置"

	
	@classmethod
	def poll(cls, context):
		global g_当前节点树
		if context.active_node :
			g_当前节点树 = context.active_node.id_data
		return context.active_node is not None

	def draw(self, context):
		layout = self.layout
		node = context.active_node
		global g_当前节点树

		for tree in bpy.data.node_groups:
			if tree.bl_idname == 'idname_场景对象操作节点树':
				layout.prop(tree, "m_isUpdate", text=tree.name)
				
		global g_是否开启节点数更新
		if g_是否开启节点数更新 :
			标题 = "关闭节点树更新"
		else:
			标题 = "开启节点树更新"
		#opera = layout.operator("node.update_node_tree_off_and_on", text=标题)
		if g_当前节点树 :
			#col = layout.column()
			#col.prop(g_当前节点树, 'm_当前绑定场景')
			col = layout.column()
			col.scale_y = 1.5
			#opera = col.operator("node.update_node_tree_passive_update", text='更新所有被动节点', icon = "PLAY")
			#opera.m_场景 = g_当前节点树.name
			if context.active_node :
				opera = col.operator("node.update_node_tree_passive_update", text='更新激活被动节点', icon = "RESTRICT_SELECT_OFF")
				opera.m_场景 = g_当前节点树.name
				opera.m_节点 = context.active_node.name
				#engineLib.flexCAPI.f_node_准备定点更新(ctypes.c_wchar_p(opera.m_节点))
				#col.scale_y = 2.5
				#opera = col.operator("node.update_node_tree_passive_update_render", text='更新*节点', icon = "PLAY")
				#opera.m_场景 = g_当前节点树.name
				#opera.m_节点 = context.active_node.name
		opera = layout.operator('node.update_reset_all_node_update_timings', text='重置所有节点更新计时')
		opera.m_场景 = g_当前节点树.name


classes = (
	C_ops_节点树更新开关,
	C_ops_更新被动节点树,

	C_ops_重新构建当前场景树,
	C_ops_重新构建所有场景树,

	C_ops_重置所有节点更新计时,


	C_ops_更新被动节点树_渲染下一帧,

	NODE_PT_active_节点树更新配置,
	NODE_PT_active_激活节点配置,

)

def register():
	#bpy.types.NodeTree.m_是否开启节点树更新 = bpy.props.StringProperty(name='是否开启节点树更新', default='')

	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)
	

#bpy.ops.wm.alembic_import()
#bpy.ops.wm.alembic_import()